#include "widget.h"
#include "ui_widget.h"

#include <QDebug>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
}

Widget::~Widget()
{
    delete ui;
}

void Widget::on_openBtn_clicked()
{
    QString strSlnFile = "/home/zqykj/ProgramData/CognitiveComputing/zqyHyjjWorkspace/org.zqykj.hyjj.Common_Crime/{d31015ca-f55f-446d-8830-6798bfa9e9df}/{d31015ca-f55f-446d-8830-6798bfa9e9df}.zqysln";
    int lockfd = open(strSlnFile.toLocal8Bit().data(), O_RDWR);
    if (lockfd == -1)
    {
        qWarning() << "open case.txt fail!";
    }
    int iret = flock(lockfd, LOCK_EX | LOCK_NB);
    if (iret != 0)
    {
        qWarning() << "lockfd" << lockfd << ":" << "case.txt lock fail!";
        ::close(lockfd);
    }
    else {
        m_iFd = lockfd;
        qWarning() << "lockfd" << lockfd << ":"<< "case.txt lock success!";
    }

    //::close(lockfd);
}

void Widget::on_closeBtn_clicked()
{
    QString strSlnFile = "/home/zqykj/ProgramData/CognitiveComputing/zqyHyjjWorkspace/org.zqykj.hyjj.Common_Crime/{d31015ca-f55f-446d-8830-6798bfa9e9df}/{d31015ca-f55f-446d-8830-6798bfa9e9df}.zqysln";
    //int lockfd = open(strSlnFile.toLocal8Bit().data(), O_RDWR);
    if (m_iFd == -1)
    {
        qWarning() << "open case.txt fail!";
    }
    int iret = flock(m_iFd, LOCK_UN);
    if (iret != 0)
    {
        qWarning() << "lockfd" << m_iFd << ":" << "case.txt unlock fail!";
    }
    else {
        qWarning() << "lockfd" << m_iFd << ":" << "case.txt unlock success!";
        ::close(m_iFd);
    }
}
